﻿using System;
using System.Text.RegularExpressions;

namespace Hxsoft
{
	/// <summary>
	/// 正则表达式扩展类。
	/// </summary>
	public static class RegexExtensions
	{
		/// <summary>
		/// 数字模式
		/// </summary>
		public static readonly string Pattern_Numerical = @"(-?)((?:[1-9]\d*)|0)(?:\.(\d*[^0]))?";

		/// <summary>
		/// 
		/// </summary>
		public static readonly string Pattern_Proportion = @"^(?:0|1|0\.[0-9]+)$";

		/// <summary>
		/// 时间日期模式
		/// </summary>
		public static readonly string Pattern_DateTime = @"^(?=\d)(?:(?!(?:1582(?:\.|-|\/)10(?:\.|-|\/)(?:0?[5-9]|1[0-4]))|(?:1752(?:\.|-|\/)0?9(?:\.|-|\/)(?:0?[3-9]|1[0-3])))(?=(?:(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:\d\d)(?:[02468][048]|[13579][26]))\D0?2\D29)|(?:\d{4}\D(?!(?:0?[2469]|11)\D31)(?!0?2(?:\.|-|\/)(?:29|30))))(\d{4})([-\/.])(0?\d|1[012])\2((?!00)[012]?\d|3[01])(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$";

		/// <summary>
		/// 邮政编码
		/// </summary>
		public static readonly string Pattern_Postcode = @"[1-9]\d{5}(?!\d)";

		/// <summary>
		/// 电话号码
		/// </summary>
		public static readonly string Pattern_PhoneCode = @"(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}";

		/// <summary>
		/// 手机号码
		/// </summary>
		public static readonly string Pattern_MobilePhoneNumber = @"1[3|5|8][0-9]{9}";

		/// <summary>
		/// Email 地址
		/// </summary>
		public static readonly string Pattern_Email = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";

		/// <summary>
		/// 网址
		/// </summary>
		public static readonly string Pattern_WebsiteUrl = @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";




		public static string ReplaceString(string str, string pattern, string repStr, RegexOptions op)
		{
			if (string.IsNullOrEmpty(str)) return str;
			Regex re = new Regex(pattern, op);
			return re.Replace(str, repStr);
		}

		public static string ReplaceString(string str, string pattern, string repStr)
		{
			return ReplaceString(str, pattern, repStr, RegexOptions.None);
		}
	}
}